package com.neo.service;

import java.io.FileNotFoundException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.neo.util.DateUtil;
import com.neo.util.DjsStringUtil;
import com.neo.util.ReadFromFile;

@Service
public class TransferProjectService {

	protected static Logger logger = LoggerFactory.getLogger(TransferProjectService.class);

	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * 读取债权信息excel,生成标的
	 * @param creditor_username
	 * @param status
	 * @throws FileNotFoundException
	 * @throws ParseException
	 */
	public void insertProject(String creditor_username,String status) throws FileNotFoundException, ParseException {
		List listCsv = ReadFromFile.readFileByLinesGbk("D:/点金石/新债权信息/uat/project.csv");
		for (int i = 1; i < listCsv.size(); i++) {
			String[] s = (String[]) listCsv.get(i);
			String project_no = s[0];// 标的编号
			String invest_rate = s[20];// 投资年化利率
			String loan_term = s[9];// 借款期限
			String loan_amt = s[17];// 借款金额
			String project_name = s[11];// 标的名称
			String idNo = s[13];
			String sign_date = s[4];// 借款协议签订时间
			String interest_pay_day = s[4];// 放款日期

			int loan_ment_day = getLoanMentDay(loan_term, sign_date);
			String borrower_account_id = null;
			String borrower_username = null;
			List<Map<String, Object>> list = jdbcTemplate
					.queryForList(" select account_id,username from jx_account_info where id_no =? ", new Object[] { idNo });
			if (list.size() > 0) {
				Map<String, Object> map = list.get(0);
				borrower_account_id = map.get("account_id") + "";
				borrower_username = map.get("username") + "";
			}
			// jdbcTemplate.queryForMap(selectSql,new Object[]{idNo});

			String sql = " replace into  jx_invest_project  (PROJECT_NO,PROJECT_NAME,LOAN_AMT,INTEREST_PAY_TYPE,"
					+ "LOAN_MENT,INVEST_RATE,BORROWER_ACCOUNT_ID,IS_SUPER_INVESTOR,PAY_NO,ENABLE_AMT,BORROWER_USERNAME,"
					+ "creditor_username,STATUS,interest_pay_day"
					+ ")"
					+ "values(?,?,?,?, ?,?,?,?,?,?,?,?,?,?)";
			int j = jdbcTemplate.update(sql, project_no, project_name, loan_amt, 1, loan_ment_day, invest_rate,
					borrower_account_id, 1, project_no, loan_amt,borrower_username,creditor_username,status,interest_pay_day);
			logger.info("更新成功："+j+"/"+i+"/"+listCsv.size());
		}
	}

	// 将期数转成天数
	private int getLoanMentDay(String loan_term, String sign_date) throws ParseException {
		int loan_ment_day = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date signDate = sdf.parse(sign_date);
		if (loan_term != null) {
			loan_ment_day = DateUtil.getLoanMent(Integer.parseInt(loan_term), signDate);
		}
		return loan_ment_day;
	}

	private int getLoanMentDayInteger(Integer loan_term, String sign_date) throws ParseException {
		int loan_ment_day = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date signDate = sdf.parse(sign_date);
		if (loan_term != null) {
			loan_ment_day = DateUtil.getLoanMent(loan_term, signDate);
		}
		return loan_ment_day;
	}

	public static void main(String[] args) throws Exception {
		System.out.println(PinyinHelper.getShortPinyin("嗨秒贷").toUpperCase());
	}

	public void updateProjectLoanMent() throws ParseException {
		Integer loan_ment = null;
		Integer id = null;
		String make_loan_time = null;
		List<Map<String, Object>> list = jdbcTemplate
				.queryForList(" select LOAN_MENT,id,LEFT(MAKE_LOAN_TIME,10) as MAKE_LOAN_TIME  from jx_invest_project where id > 1 ", new Object[] {});

		if (list.size() == 0) {
			return;
		}
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = list.get(i);
			loan_ment = (Integer) map.get("LOAN_MENT");
			id = (Integer) map.get("id");
			make_loan_time = (String) map.get("MAKE_LOAN_TIME");
			int loan_ment_day = getLoanMentDayInteger(loan_ment, make_loan_time);
			String sql = " update jx_invest_project set LOAN_MENT=? where id=? ";
			int j = jdbcTemplate.update(sql,loan_ment_day,id);
		}
	}
}
